package com.zzh.myapplication.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.icarplus.car.R;
import net.icarplus.car.adapter.CarListAdapter;
import net.icarplus.car.bean.CarImageBean;
import net.icarplus.car.bean.CarInfo;
import net.icarplus.car.bean.CarListBean;
import net.icarplus.car.bean.CarPriceBean;
import net.icarplus.car.bean.ResultInfo;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.views.WaterDropListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/8 10:37
 * 修改人：Gophy
 * 修改时间：16/3/8 10:37
 * 修改备注：
 */
public class DivideTimeCarActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, WaterDropListView.IWaterDropListViewListener, AdapterView.OnItemClickListener {
    private TextView tvMap;
    private WaterDropListView waterDropListView;
    private CarListAdapter adapter;
    private ArrayList<CarListBean> lists = new ArrayList<CarListBean>();
    private CarListBean model;
    private LoadingUtil loadingUtil;
    private JSONArray array;
    private JSONObject jsons;
    private int pageNum = 1;
    private boolean isRefresh = false;
    private RadioGroup rgTabSelect;
    private String gearType = "01";
    public Toolbar toolbar;
    private List<CarInfo> savaCarInfo = new ArrayList<CarInfo>();

    public static ResultInfo carResultInfo;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    waterDropListView.stopRefresh();
                    break;
                case 2:
                    waterDropListView.stopLoadMore();
                    break;
                case 1000:
                    DivideTimeCarActivity.this.loadingUtil.stopShowLoading();
                    Log.e("1000", msg.obj.toString());
                    refreshCarList(msg.obj.toString());
                    break;
                case 3000:
                    Log.e("3000", "3000000");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void setContent() {
        setContentView(R.layout.activity_divide_time_car);
    }

    @Override
    public void initComponent() {
        tvMap = (TextView) findViewById(R.id.tv_menu);
        waterDropListView = (WaterDropListView) findViewById(R.id.lv_car_list);
        rgTabSelect = (RadioGroup) findViewById(R.id.tab_select);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.time_car_text));

    }

    @Override
    public void setListener() {
        waterDropListView.setWaterDropListViewListener(this);
        waterDropListView.setPullLoadEnable(true);
        rgTabSelect.setOnCheckedChangeListener(this);
        tvMap.setOnClickListener(this);
        waterDropListView.setOnItemClickListener(this);
    }

    @Override
    public void onAfterInit() {
        this.loadingUtil = new LoadingUtil(this, "查询中...");
//        adapter = new CarListAdapter(DivideTimeCarActivity.this, lists);
        this.loadingUtil.startShowLoading();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_menu:
                break;
            case R.id.iv_order_pay:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();

        if (savaCarInfo.size() > 0) {
            savaCarInfo.clear();
        }

//        setCarListsParams(gearType, pageNum);
        this.loadingUtil.startShowLoading();
        PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");

    }


    @Override
    public void onRefresh() {
        if (savaCarInfo.size() > 0) {
            savaCarInfo.clear();
        }
        pageNum = 1;
        this.loadingUtil.startShowLoading();
        PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");
        handler.sendEmptyMessage(1);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        this.loadingUtil.startShowLoading();
        PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");
        handler.sendEmptyMessage(2);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_oil_auto:
                if (savaCarInfo.size() > 0) {
                    savaCarInfo.clear();
                }
                gearType = "02";
                pageNum = 1;
                this.loadingUtil.startShowLoading();
                PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");
                break;
            case R.id.rb_oil_manual:
                if (savaCarInfo.size() > 0) {
                    savaCarInfo.clear();
                }
                gearType = "01";
                pageNum = 1;
                this.loadingUtil.startShowLoading();
                PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");
                break;
            case R.id.rb_ele_auto:
                if (savaCarInfo.size() > 0) {
                    savaCarInfo.clear();
                }
                gearType = "02";
                pageNum = 1;
                this.loadingUtil.startShowLoading();
                PublicRequest.queryNearbyCars(this, handler, pageNum, gearType, "01");
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        CarInfo carInfo = null;
        for (int i = 0; savaCarInfo != null && i < savaCarInfo.size(); i++) {
            carInfo = savaCarInfo.get(position - 1);
            List<CarImageBean> carImage = carInfo.carImgList;
            List<CarPriceBean> carPrice = carInfo.priceList;
            CarImageBean carImageBean = null;
            CarPriceBean carPriceBean = null;
            for (int j = 0; carImage != null && j < carImage.size(); j++) {
                carImageBean = carImage.get(j);
            }
            for (int j = 0; carPrice != null && j < carPrice.size(); j++) {
                carPriceBean = carPrice.get(j);
            }
            Intent intent = new Intent(DivideTimeCarActivity.this, DivideTimeCarListDetailActivity.class);
            intent.putExtra("title", carInfo.modelName);
            intent.putExtra("unitPrice", carPriceBean.unitPrice);
            intent.putExtra("picUrl", UrlValues.CAR_IMG_ID + carImageBean.imgId);
            intent.putExtra("licenseNo", carInfo.licenseNo);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            start_Activity(intent);
        }
    }

    private void refreshCarList(String carList) {
        if (carList != null) {

            Gson gson = new Gson();
            ResultInfo result = gson.fromJson(carList,
                    new TypeToken<ResultInfo>() {
                    }.getType());
            carResultInfo = result;
            savaCarInfo = result.queryList;
            adapter = new CarListAdapter(DivideTimeCarActivity.this, savaCarInfo);
            adapter.notifyDataSetChanged();
            waterDropListView.setAdapter(adapter);

        }
    }


}
